import { axios } from '@/utils/request'
import ActivityDict from './dictItem/activity'
import MessageDict from './dictItem/message'
import CommonDict from './dictItem/common'
import MemberDict from './dictItem/member'
import OrderDict from './dictItem/order'

/**
 * 字典项 item
 * @type {dictItem}
 * dictLabel: string;
 * dictValue: string;
 * dictType: string;
 * dictCode: number;
 */

/**
 * 例:
 *  const dicts = await dictionary.getMulti(['hydw_zyyw'])
 *  dicts.hydw_zyyw ===> dictItem
 */

/**
 * @description 字典类
 * @date 04/07/2022
 * @class Dictionary
 */
class Dictionary {
  constructor() {
    /**
     * 字典Map集合
     */
    this.dictAll = new Map([...ActivityDict, ...MessageDict, ...CommonDict, ...MemberDict, ...OrderDict])
  }

  /**
   * 添加单个本地字典项
   * @param {string} key
   * @param {dictItem[]} val
   * @memberof Dictionary
   */
  add(key, val) {
    this.dictAll.set(key, val)
  }

  /**
   * 获取本地字典项
   * @param {*} key
   * @return {dictItem[] | []}
   * @memberof Dictionary
   */
  getLocal(key) {
    let val = this.dictAll.get(key)
    return val ? val : []
  }

  getLocalMap(key, name, value) {
    let val = this.dictAll.get(key)
    return val ? val.map(item => ({ [name]: item.dictLabel, [value]: item.dictValue })) : []
  }
  /**
   * 获取单个字典项 (不存在即取查询服务器字典)
   * @param {string} key
   * @return {dictItem[] | []}
   * @memberof Dictionary
   */
  async get(key) {
    if (!this.hasKey(key)) {
      const res = await this.queryServerDict(key)
      if (!res[key]) return []
      this.add(key, res[key])
    }
    let val = this.dictAll.get(key)
    return val ? val : []
  }

  /**
   * 获取多个字典项 (不存在即查询服务器字典)
   * @param {string[]} key
   * @return {{[x: string]: dictItem[]}}
   * @memberof Dictionary
   */
  async getMulti(key) {
    let obj = {}
    let noneKeys = []
    key.forEach(v => (this.hasKey(v) ? (obj[v] = this.dictAll.get(v)) : noneKeys.push(v)))
    if (noneKeys.length > 0) {
      const res = await this.queryServerDict(noneKeys)
      for (const k in res) {
        const val = res[k]
        obj[k] = val
        this.add(k, val)
      }
    }
    return obj
  }

  /**
   * 查找字典项
   * @param {string} key
   * @return {boolean}
   * @memberof Dictionary
   */
  hasKey(key) {
    return this.dictAll.has(key)
  }

  /**
   * 删除字典项
   * @param {string} key
   * @return {boolean}
   * @memberof Dictionary
   */
  remove(key) {
    return this.hasKey(key) && this.dictAll.delete(key)
  }

  /**
   * 获取字典大小
   * @return {number}
   * @memberof Dictionary
   */
  size() {
    return this.dictAll.size
  }

  /**
   * 是否为空
   * @return {boolean}
   * @memberof Dictionary
   */
  isEmpty() {
    return this.size() === 0
  }

  /**
   * 获取键数组
   * @return {string[]}
   * @memberof Dictionary
   */
  keys() {
    return Array.from(this.dictAll.keys())
  }

  /**
   * 获取字典键值数组
   * @return {[string, dictItem[]][]}
   * @memberof Dictionary
   */
  keyValues() {
    return Array.from(this.dictAll.entries())
  }

  /**
   * 获取字典的对应key的对象
   * @return {[string, dictItem[]][]}
   * @memberof Dictionary
   */
  getDictItem(dictList, dictValue) {
    const dictItem = dictList.find(v => v.dictValue === dictValue)
    return dictItem || {}
  }

  /**
   * 请求服务器字典
   * @param {string | Array<string>} key
   * @return {{key: dictItem}}
   * @memberof Dictionary
   */
  queryServerDict(key) {
    let dictTypes = Array.isArray(key) ? key.join(',') : key
    return axios({
      url: 'system/dict/data/getDataList',
      method: 'get',
      params: {
        dictTypes
      }
    }).then(r => r.code === 0 && r.data)
  }
}

const dictionary = new Dictionary()
export default dictionary
